DZApp.service(
				'orderReportService',
				function(commonService) {
					return {
						// 查询主服务
						queryMainSeviceSelect : function() {
							var url = "/operationService/listLevel1OrderServiceInfo";
							return commonService.commonAjaxByPost(url);
						},
						query : function(postData) {
							var url = "/operationService/queryOrderResport";
							return commonService
									.commonAjaxByPost(url, postData);
						},
						listMerchantByManagerId : function(postData) {
							var url = "/operationService/listMerchantByManagerId";
							return commonService
									.commonAjaxByPost(url, postData);
						},
						/**
						 * 处理跨度的时间点
						 */
						getAfterDays : function(starttime) {
							// 一天
							var oneDay = 1000 * 60 * 60 * 24;
							// 跨度为7一周
							var betweenTime = oneDay * 7;
							return new Date(starttime + betweenTime);
						},
						/**
						 * 格式化输出
						 */
						dateFmt : function(date, fmt) {
							var o = {
								"M+" : date.getMonth() + 1, // 月份
								"d+" : date.getDate(), // 日
								"h+" : date.getHours(), // 小时
								"m+" : date.getMinutes(), // 分
								"s+" : date.getSeconds(), // 秒
								"q+" : Math.floor((date.getMonth() + 3) / 3), // 季度
								// 毫秒
								"S" : date.getMilliseconds()
							};
							if (/(y+)/.test(fmt))
								fmt = fmt.replace(RegExp.$1, (date
										.getFullYear() + "")
										.substr(4 - RegExp.$1.length));
							for ( var k in o)
								if (new RegExp("(" + k + ")").test(fmt))
									fmt = fmt
											.replace(
													RegExp.$1,
													(RegExp.$1.length == 1) ? (o[k])
															: (("00" + o[k])
																	.substr(("" + o[k]).length)));
							return fmt;
						},
						validate : function($scope) {
							var startDate = $scope.orderSearchModel.startDate;
							var endDate = $scope.orderSearchModel.endDate;
							if(!!endDate && !!startDate &&new Date(startDate).getTime()-new Date(endDate).getTime()  >0){
								bootboxWindow.alert('开始日期不能小于结束日期', null);
								return false;
							}
							if(!!!$scope.isNeedConditionValidate){
								return true;
							}
							if (!startDate ) {
								bootboxWindow.alert('开始日期不能为空', null);
								return false;
							}						
							if ( !endDate) {
								bootboxWindow.alert('结束日期不能为空', null);
								return false;
							}
							return true;
						},
						dealEndTimeService : function($scope) {
							if(!!!$scope.isNeedConditionValidate){
								return true;
							}
							var startDate = $scope.orderSearchModel.startDate;
							var endDate = $scope.orderSearchModel.endDate;
							if (!startDate) {
								bootboxWindow.alert('开始日期不能为空', null);
								return false;
							}
							var sD = new Date(startDate).getTime();
							var endDate = this.getAfterDays(sD);
							var datetimeRgx = /^[1-9](\d{3})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/;
							if (!startDate.match(datetimeRgx)) {
								bootboxWindow.alert('开始日期格式不正确！', null);
								return false;
							}
							$scope.orderSearchModel.endDate = this.dateFmt(
									endDate, "yyyy-MM-dd");
							return true;
						}
					};
				});
DZApp.controller('orderReportController', function($scope, orderReportService,
		$state) {
	$scope.orderSearchModel = {};
	$scope.orderItemlist = {};
	$scope.queryMainSeviceSelect = {};
	$scope.queryMainSeviceSelect = {};
	$scope.query = {};
	$scope.isNeedConditionValidate = false;
	var pagation ={
			prePageNum:20,
			curPageNo:1,
			param:null
	};
	// 翻页出发分析
	$scope.onPageChange = function() {
		var item;
		var itemtr
		for(index in $scope.orderItemlist){
			item=$scope.orderItemlist[index];
			itemtr=$("#"+item.orderNo+"");
			if(itemtr!=null){
				itemtr.remove();
			}
		}
		pagation.curPageNo = $scope.currentPage;
		queryPagination($.param(pagation));
	};
	// 分页回调函数
	var doResponseCallback = function(data) {
		if ("success" == data.retCode) {
			var data  = eval(data.bizData);
			$scope.query = data.list;
			$scope.orderItemlist = data.orderItemlist;
			$scope.pageCount = data.totalPage;
		} else {
			bootboxWindow.alert('警告查询错误:' + data.errorMsg, null);
		}
	};
	// 參數封裝
	var doParam = function(){
		pagation.param = JSON.stringify($scope.orderSearchModel);
		return $.param(pagation);
	}
	// 分页查询
	var queryPagination = function(param){
		$scope.query = null;
//		$('#table_1').DataTable().destroy();
		orderReportService.query(param).then(doResponseCallback);
	}
	
	// 是否初始化的就查询
	var isQueryInit = true;
	if (isQueryInit) {
		queryPagination(doParam());
	}
	orderReportService.queryMainSeviceSelect().then(function(data) {
		$scope.queryMainSeviceSelect = eval(data.bizData);
	});
	orderReportService.listMerchantByManagerId().then(function(data) {
		$scope.listMerchantByManagerId = eval(data.bizData);
	});
	$scope.theObj = null;
	/**
	 * 点击开始时间
	 */
	$scope.onClickStartTime = function() {
		orderReportService.dealEndTimeService($scope);
	}

	// 查詢
	$scope.queryOrderResport = function() {
		// 校验
		if (!bValidateObj.bValidateCall($('#queryOrderResportSerchModel')) || !orderReportService.validate($scope)) {
			return;
		}
//		$('#table_1').DataTable().destroy();
		$scope.query = null;
		queryPagination(doParam());
	}
	// 点击订单查看订单详情
	$scope.queryOrderItem = function($event,orderInfoList,orderNo) {
		var obj=$($event.currentTarget);
		var attr=obj.attr("ishow");
		if(attr=="true"){
			obj.next("tr").remove();
			obj.attr("ishow","false");
		}else{
			var table='<tr id="'+orderNo+'"><td colspan="8"><table class="table table-striped table-bordered table-hover" "><thead><tr>';
			table+='　<th>子服务名称</th> <th>数量</th> <th>单价</th> <th>总价</th>  <th>实际价格</th> <th>开始时间</th> <th>结束时间</th><th>运营商</th>　</thead>'
			var item;
			for(index in $scope.orderItemlist){
				item=$scope.orderItemlist[index];
				if(item.orderNo==orderNo){
					table+='<tr class="odd gradeX">';
					if(item.serviceSubNo!=undefined){
						table+='<td><span>'+item.serviceSubName+'</span></td>'
					}else{
						table+='<td><span></span></td>'
					}
					if(item.buyNum!=undefined){
						table+='<td><span>'+item.buyNum+'</span></td>'
					}else{
						table+='<td><span></span></td>'
					}
					if(item.unitPrice!=undefined){
						table+='<td><span>'+item.unitPrice+'</span></td>'
					}else{
						table+='<td><span></span></td>'
					}
					if(item.totalPrice!=undefined){
						table+='<td><span>'+item.totalPrice+'</span></td>'
					}else{
						table+='<td><span></span></td>'
					}
					if(item.realTotalPrice!=undefined){
						table+='<td><span>'+item.realTotalPrice+'</span></td>'
					}else{
						table+='<td><span></span></td>'
					}
					if(item.startTime!=undefined){
						table+='<td><span>'+item.startTime+'</span></td>'
					}else{
						table+='<td><span></span></td>'
					}
					if(item.endTime!=undefined){
						table+='<td><span>'+item.endTime+'</span></td>'
					}else{
						table+='<td><span></span></td>'
					}
					if(item.serviceProvider!=undefined){
						for(index in $scope.listMerchantByManagerId){
							merchant=$scope.listMerchantByManagerId[index];
							if(item.serviceProvider==merchant.merchantCode){
								table+='<td><span>'+merchant.shortName+'</span></td>'
							}
							
						}
					}else{
						table+='<td><span></span></td>'
					}
					table+='</tr>';
				}
			}
			table+=' </table></td></tr>';
			obj.after(table);
			obj.attr("ishow","true");
		}
	}
});
